<template>
  <div class="app-container">
    <!-- 查询对话框   -->
    <el-dialog :title="title" v-model="openSearch" :close-on-click-modal="false" draggable width="580px" append-to-body>
      <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="98px">
        <el-col :span="24">
          <el-form-item label="生产日期">
            <el-date-picker
              style="width: 350px"
              v-model="daterangeScrq"
              value-format="yyyy-MM-dd HH:mm:ss"
              type="daterange"
              range-separator="-"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              :picker-options="pickerOptions"
              :default-time="['00:00:00', '23:59:59']"></el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="机台编号" prop="machineCode">
            <el-input v-model="queryParams.machineCode" placeholder="请输入机台编号" clearable style="width: 350px" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="制造单号" prop="madeNo">
            <el-input v-model="queryParams.madeNo" placeholder="请输入制造单号" clearable style="width: 350px" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="销售单号" prop="marketNo">
            <el-input v-model="queryParams.marketNo" placeholder="请输入销售单号" clearable style="width: 350px" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="综合查询" prop="itemCode">
            <el-input v-model="queryParams.itemCode" placeholder="请输入客户编码或者物料代码" clearable style="width: 350px" />
          </el-form-item>
        </el-col>
        <el-col :span="24" style="text-align: center">
          <el-radio-group v-model="queryParams.shiftCode">
            <el-radio label="BB">白班</el-radio>
            <el-radio label="YB">夜班</el-radio>
            <el-radio label="">白班夜班</el-radio>
          </el-radio-group>
        </el-col>
        <el-col :span="24">
          <el-form-item style="margin-top: 25px; display: flex; justify-content: flex-end">
            <el-button @click="cancel">取消</el-button>
            <el-button type="primary" @click="handleQuery">确认</el-button>
          </el-form-item>
        </el-col>
      </el-form>
    </el-dialog>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-search" @click="handleSearch" size="small">查询</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          @click="handleExportByRearEnd"
          v-hasPermi="['quality:badProdAnalyse:exportReport']"
          size="small">
          明细数据
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          @click="handleExport('#badAnalyseTop10', '不良统计分析前10项物料不良')"
          v-hasPermi="['quality:badProdAnalyse:exportReport']"
          size="small">
          前10项物料不良
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          @click="handleExport('#badAnalyseTotal', '不良品统计分析不良数量统计')"
          v-hasPermi="['quality:badProdAnalyse:exportReport']"
          size="small">
          前10项物料不良
        </el-button>
      </el-col>
      <el-col :span="2.2" style="position: absolute; right: -10px; top: 0">
        <Page
          v-if="activeTab === 'first'"
          style="margin-right: 0px"
          :current="queryParams.pageNum"
          :page-size="queryParams.pageSize"
          :total="total1"
          @on-change="pageChange"
          simple></Page>
        <!--        <Page
          v-if="activeTab === 'second'"
          style="margin-right: 0px"
          :current="queryParams.pageNum"
          :page-size="queryParams.pageSize"
          :total="total2"
          @on-change="pageChange"
          simple></Page>-->
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <el-tabs v-model="activeTab">
      <el-tab-pane label="明细数据" name="first">
        <el-table border stripe id="badAnalyseDetail" :height="tableH" :data="badAnalyseList" :header-cell-style="{ 'text-align': 'center' }">
          <el-table-column label="#" type="index" width="50" align="center" fixed="left" />
          <el-table-column label="生产日期" align="center" prop="blmScrq" width="80" />
          <el-table-column label="班次" align="center" prop="blmBcdm" width="80" />
          <el-table-column label="机台" align="center" prop="blmJtbh" width="80" />
          <el-table-column label="制造单号" align="center" prop="moeZzdh" width="120" />
          <el-table-column label="销售单号" align="center" prop="moeSodh" width="120" />
          <el-table-column label="物料代码" align="center" prop="moeWldm" width="120" />
          <el-table-column label="品名规格" align="center" prop="itmPmgg" />
          <el-table-column label="模具编号" align="center" prop="moeMjbh" />
          <el-table-column label="不良代码" align="center" prop="blmBldm" width="80" />
          <el-table-column label="不良描述" align="center" prop="blmBlms" width="80" />
          <el-table-column label="数量" align="center" prop="blmBlsl" width="80" />
          <el-table-column label="建立人员" align="center" prop="blmJlry" width="80" />
          <el-table-column label="建立日期" align="center" prop="blmJlrq" width="120" />
        </el-table>
      </el-tab-pane>
      <el-tab-pane label="前10项物料不良" name="second">
        <el-table border stripe id="badAnalyseTop10" :height="tableH" :data="badAnalyseTop10" :header-cell-style="{ 'text-align': 'center' }">
          <el-table-column label="#" type="index" width="50" align="center" fixed="left" />
          <el-table-column label="不良代码" align="center" width="120" prop="blmBldm" />
          <el-table-column label="不良描述" align="center" width="120" prop="itmCustwldm" />
          <el-table-column label="物料品名" align="center" width="300" prop="itmPmgg" />
          <el-table-column label="数量" align="center" width="120" prop="blmBlsl" />
        </el-table>
      </el-tab-pane>
      <el-tab-pane label="不良数量统计" name="third">
        <el-table border stripe id="badAnalyseTotal" :height="tableH" :data="badAnalyseTotal" :header-cell-style="{ 'text-align': 'center' }">
          <el-table-column label="#" type="index" width="50" align="center" fixed="left" />
          <el-table-column label="生产日期" align="center" width="100" prop="blmScrq" />
          <el-table-column label="不良代码" align="center" width="120" prop="blmBldm" />
          <el-table-column label="不良描述" align="center" width="120" prop="blmBlms" />
          <el-table-column label="数量" align="center" width="130" prop="blmBlsl" />
        </el-table>
      </el-tab-pane>
    </el-tabs>
  </div>
</template>

<script>
import { exportTable } from '@/utils/ruoyi'
import { exportBadAnalyseReport, listBadAnalyseReport } from '@/api/quality/badAnalyse'

export default {
  name: 'BadAnalyseReport',
  data() {
    return {
      // 遮罩层
      loading: true,
      tableH: document.body.clientHeight - 300,
      activeTab: 'first',
      badAnalyseList: [],
      badAnalyseTop10: [],
      badAnalyseTotal: [],
      // 显示搜索条件
      openSearch: false,
      showSearch: true,
      // 总条数
      total1: 0,
      total2: 0,
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 建立日期时间范围
      daterangeScrq: [new Date(new Date().getTime() - 3600 * 1000 * 24 * 30), this.$moment().endOf('month').toDate()],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 25,
        dateBegin: null,
        dateEnd: null,
        machineCode: null,
        madeNo: null,
        marketNo: null,
        itemCode: null,
        shiftCode: '',
      },
      // 表单参数
      form: {},
      //日期初始化
      pickerOptions: {
        shortcuts: [
          {
            text: '最近一周',
            onClick(picker) {
              const end = new Date()
              const start = new Date()
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7)
              picker.$emit('pick', [start, end])
            },
          },
          {
            text: '最近一个月',
            onClick(picker) {
              const end = new Date()
              const start = new Date()
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30)
              picker.$emit('pick', [start, end])
            },
          },
          {
            text: '最近三个月',
            onClick(picker) {
              const end = new Date()
              const start = new Date()
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 90)
              picker.$emit('pick', [start, end])
            },
          },
        ],
      },
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询不良品统计分析列表 */
    getList() {
      this.loading = true
      if (null != this.daterangeScrq && '' != this.daterangeScrq) {
        this.queryParams.dateBegin = this.moment(this.daterangeScrq[0]).format('YYYY-MM-DD HH:mm:ss')
        this.queryParams.dateEnd = this.moment(this.daterangeScrq[1]).format('YYYY-MM-DD HH:mm:ss')
      }
      listBadAnalyseReport(this.queryParams)
        .then((response) => {
          // 明细
          this.badAnalyseList = response.data.detail.rows
          this.total1 = response.data.detail.total
          // 前10项不良统计
          this.badAnalyseTop10 = response.data.top10
          // this.total2 = response.data.top10.total
          // 统计
          this.badAnalyseTotal = response.data.total

          this.loading = false
        })
        .catch((error) => {
          console.log(error)
          this.loading = false
        })
    },
    handleSearch() {
      this.openSearch = true
      this.title = '查询'
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.openSearch = false
      this.reset()
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
      this.openSearch = false
    },
    // 表单重置
    reset() {
      this.form = {
        machineCode: null,
        madeNo: null,
        marketNo: null,
        itemCode: null,
        shiftCode: null,
      }
      this.resetForm('form')
    },
    pageChange(page) {
      this.queryParams.pageNum = page
      this.getList()
    },
    /** 导出到Excel（前端导出） **/
    handleExport(a, b) {
      this.$confirm('是否确认导出' + b + '?', '确认', {
        cancelButtonText: '取消',
        confirmButtonText: '确认',
        type: 'warning',
      })
        .then(() => {
          this.exportLoading = true
          return exportTable(a, b)
        })
        .then(() => {
          this.exportLoading = false
        })
    },
    /** 导出到Excel（后端导出） **/
    handleExportByRearEnd() {
      const queryParams = this.queryParams
      this.$confirm('是否确认导出所有不良品统计分析明细数据？', '确认', {
        cancelButtonText: '取消',
        confirmButtonText: '确定',
        type: 'warning',
      })
        .then(() => {
          this.exportLoading = true
          return exportBadAnalyseReport(queryParams)
        })
        .then((response) => {
          this.download(response.msg)
          this.exportLoading = false
        })
        .catch(() => {})
    },
  },
}
</script>

<style scoped></style>
